package util.src.com;

import sun.plugin2.os.windows.Windows;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author zz
 */

public class Download {
    public String url;
    public String path;
    public String name;
    /**
     * public int connectTimeout = 0;
     */
    public String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36";
    public String origin = "https://zmee44.com";
    public String referer = "https://zmee44.com/";
    public String Host= "video.feimanzb.com:8091";
    public String Accept = "*/*";
    public String Connection= "keep-alive";

    public String CacheControl = "max-age=0";
    public String Range = "bytes=0-";

    public Download(String url, String path, String name) {
        this.url = url;
        this.path = path;
        this.name = name;
    }

    public void download() throws Exception {

        URL realUrl = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();

/*
        connection.setRequestMethod("get");
        connection.setRequestProperty("Host", Host);
        connection.setRequestProperty("Connection", Connection);
        connection.setRequestProperty("User-Agent", userAgent);
        connection.setRequestProperty("Accept", Accept);
        connection.setRequestProperty("origin", origin);
        connection.setRequestProperty("Sec-Fetch-Site", "cross-site");
        connection.setRequestProperty("Sec-Fetch-Mode", "cors");
        connection.setRequestProperty("Sec-Fetch-Dest", "empty");
        connection.setRequestProperty("referer", referer);
        connection.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
        connection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
*/
        connection.setRequestProperty("Upgrade-Insecure-Requests", "1");

        referer = "https://www.ggzm10.com/video/show/id/14781";
        userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36";
        //connection.setRequestProperty("Referer", referer);
        //connection.setRequestProperty("Range", Range);
        //connection.setRequestProperty("Cache-Control", CacheControl);

        //connection.setRequestProperty("User-Agent", userAgent);

        //connection.setConnectTimeout(30 * 1000);
        System.out.println(11111111);
        System.out.println(connection.getResponseCode());

        connection.connect();


        int code = 200;
        System.out.println(connection.getResponseCode());
        if (connection.getResponseCode() != 404) {
            // 正常响应
            // 从流中读取响应信息
            BufferedInputStream reader = new BufferedInputStream(connection.getInputStream());
            BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(path + name));
            byte[] buffer = new byte[1024];
            int len;

            while ((len = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, len);
                writer.flush();

            }
            writer.flush();

            writer.close();
            reader.close(); // 关闭流
        }

    }

    public static void main(String[] args) throws Exception {
        Download a = new Download("https://www.google.com/", "D:\\乱七八糟\\Downloads\\5", "1.txt");
        a.download();

    }

}
